<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Skit\Ctl\SkitTheaterCtl;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterDelDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTheaterListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Skit\SkitTheaterListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class SkitTheaterAct extends ActionAbstract
{
    /**
     * @url /skit/theater/list
     * @auth skitTheaterList
     * @req Srv.Apps.Proto.DTO.Skit.SkitTheaterListDTO
     * @rsp Srv.Apps.Proto.VO.Skit.SkitTheaterListVO
     * 剧场信息列表
     */
    public function getSkitTheaterList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof SkitTheaterListDTO) || !($VOClass instanceof SkitTheaterListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = SkitTheaterCtl::tag()->getSkitTheaterList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /skit/theater/add
     * @auth skitTheaterAdd
     * @req Srv.Apps.Proto.DTO.Skit.SkitTheaterAddDTO
     * 剧场信息新增
     */
    public function skitTheaterAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitTheaterAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        $thcfId         = SkitTheaterCtl::tag()->skitTheaterAdd($setData);
        if($thcfId < 1){
            $errMsg     = '剧场信息新增失败';
            $LogCtl->logAdd(LogCtl::SKIT_THEATER_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '剧场信息新增成功';
        $LogCtl->logAdd(LogCtl::SKIT_THEATER_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/theater/edit
     * @auth skitTheaterEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitTheaterEditDTO
     * 剧场信息编辑
     */
    public function skitTheaterEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitTheaterEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $thcfId         = intval($DTOClass->getThcfId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitTheaterCtl = SkitTheaterCtl::tag();
        $theaterSrc     = $SkitTheaterCtl->getSkitTheaterSrc($thcfId);
        if(!isset($theaterSrc['thcf_id'])) {
            $logData    = $setData;
            $errMsg     = '剧场信息不存在';
            $LogCtl->logAdd(LogCtl::SKIT_THEATER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $SkitTheaterCtl->skitTheaterEdit($thcfId, $setData);
        $logData        = ['new' => $setData, 'old' => $theaterSrc];
        if(!$result){
            $errMsg     = '剧场信息不存在';
            $LogCtl->logAdd(LogCtl::SKIT_THEATER_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '剧场信息编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_THEATER_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/theater/del
     * @auth skitTheaterDel
     * @req Srv.Apps.Proto.DTO.Skit.SkitTheaterDelDTO
     * 剧场信息删除
     */
    public function skitTheaterDel(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitTheaterDelDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $thcfId         = intval($DTOClass->getThcfId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $logData        = $setData;
        $LogCtl         = LogCtl::tag();
        $SkitTheaterCtl = SkitTheaterCtl::tag();
        $theaterSrc     = $SkitTheaterCtl->getSkitTheaterSrc($thcfId);
        if(!isset($theaterSrc['thcf_id'])) {
            $errMsg     = '剧场信息不存在';
            $LogCtl->logAdd(LogCtl::SKIT_THEATER_DEL, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $SkitTheaterCtl->skitTheaterDel($thcfId);
        if(!$result) {
            $errMsg     = '剧场信息删除失败';
            $LogCtl->logAdd(LogCtl::SKIT_THEATER_DEL, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '剧场信息删除成功';
        $LogCtl->logAdd(LogCtl::SKIT_THEATER_DEL, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}